package com.ydlclass;

import com.ydlclass.discovery.Registry;
import com.ydlclass.exceptions.NetworkException;
import com.ydlclass.proxy.handler.RpcConsumerInvocationHandler;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @Author duWenJian
 * @Date 2024/2/10 17:07
 * @Version 1.0
 */
@Data
@NoArgsConstructor
@Slf4j
public class ReferenceConfig<T> {

    private Class<T> Interface;

    private Registry registry;

    private String group = "default";

    public Registry getRegistry() {
        return registry;
    }

    public void setRegistry(Registry registry) {
        this.registry = registry;
    }

    /**
     *
     * 代理设计模式，生成一个api接口的代理对象，helloYrpc.sayHi("你好");
     * @return 代理对象
     */
    public T get() {
        // 此处一定是使用动态代理完成一些工作，获取当前线程上下文的ClassLoader
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        Class[] classes = new Class[]{Interface};
        InvocationHandler handler = new RpcConsumerInvocationHandler<>(Interface, registry, group);
        // 使用动态代理来生成代理对象，
        Object helloProxy = Proxy.newProxyInstance(classLoader, classes, handler);
        return (T) helloProxy;
    }
}
